#===============================================================================
# Application.mk
#
# Master makefile for applications using CBT++ 
# You can build without using this makefile easily enough.  This makefile 
# just automates my standard way of creating new applications. 
#
# This is the master makefile.  In each subdirectory where you want
# to create some new application based on the CBT++ libraries you 
# can create a makefile that includes this one and lists any local 
# classes found in that subdirectory. See $(CBT_ROOT)/makefiles/Application.sample.mk 
# for an example. The local makefiles find this master make file via the CBT_ROOT 
# environment variable (e.g. export CBT_ROOT=~/CBT++ or wherever you have it installed). 
#
# In each application subdirectory, to make an application called foo from 
# a source file called foo.cc, compiling local libraries Bar.cc BarBar.cc, you will 
# need to list Bar and BarBar on the libtargets line like this:
#
# libtargets = Bar BarBar
#
# Then do Make foo.   That will compile foo.cc, Bar.cc, BarBar.cc, and link 
# them with libcbt (CBT++) to generate an application called foo. 
# There are several variations on this to enable debugging, profiling, 
# to link in oracle if the application needs oracle libraries, to link in 
# an alternate STL, STLPort in either debugging mode (safe, which detects 
# out of boundary conditions) or fast mode.  In the future these might be 
# implemented with -D options, but for now they are implemented by adding
# a suffix to foo in the Make command, as follows:
# 
# make foo             Compile foo.cc and any libraries listed on libtargets line. -O3
# make foo.g           Compile foo.cc and libtargets with debugging on.  
# make foo.pg          Compile foo.cc and libtargets with debug and profiling on. 
# make foo.oracle      Compile foo.cc and links in the oracle libraries (slow). -O3
# make foo.stlport     Compile foo.cc using the stlport library with safestl option on. 
# make foo.faststlport Compile foo.cc using stlport library with fast options. 
#
#
# This should work on either linux or Solaris.  There are a few differences
# when working on OSX.   On an OSX machine, make sure that OSTYPE=osx-apple. 
# That is, put this in your .bashrc:
#
# export OSTYPE=osx-apple
#
#
# 
#------------------------------------------------------------------------------
# K. James Durbin    					2/28/03
#===============================================================================
#

#=============================================================================
# Setup Paths
#=============================================================================

# Where are all of the CBT++ sources, includes, and libraries  kept? 
#include $(CBT_ROOT)/makefiles/Makefile.in

#include $(CBT_ROOT)/makefiles/ForteApp.mk
#include $(CBT_ROOT)/makefiles/STLPortApp.mk

# Where are all of the HGSC++ sources, includes, and libraries kept?
#include $(HGSC_ROOT)/Makefile.in

#=============================================================================
# Compiler flags
#=============================================================================
#CCFLAGS = -g -pg -DCBT
#CCFLAGS = -O3 -DCBT
#CCFLAGS = -g -DCBT

ifeq ($(OSTYPE),osx-apple)
CCFLAGS = -DCBT 
LINKFLAGS = 
else
CCFLAGS = -DCBT -static
LINKFLAGS = -static
endif


#=============================================================================
#	Create compile options out of supplied include and library paths. 
#=============================================================================


#=============================================================================
#	Create compile options out of supplied include and library paths. 
#=============================================================================


ifeq ($(GCCVERSION),ICC)

# STLPORT 
#INCLUDES_CC = -I/local_home/kdurbin/include/stlport/ -I$(CBT_INC) $(appincludes)
#LIB_PATHS =  -L/local_home/kdurbin/lib/STLport-4.6.2/lib -L$(CBT_LIB) 

# ICC STL
INCLUDES_CC = -I$(CBT_INC) $(appincludes)
LIB_PATHS =  -L$(CBT_LIB) 

CCFLAGS = -pthread -DCBT -static

# These options are for GCC compatibility mode, which is nice
# but which doesn't give us the 64-bit space we need. 
#INCLUDES_CC = -I$(CBT_INC) -I/usr/include/ -I/usr/include/c++/3.2.3/ -I/usr/include/c++/3.2.3/ext/ $(appincludes)
#LIB_PATHS = -L$(CBT_LIB) -L/usr/lib/
#LINKFLAGS = -static -cxxlib-gcc 

LIBS = -lgzstream -lz 

#LIBS = -lstlport_icc 

else
INCLUDES_CC = -I$(CBT_INC) $(appincludes)
LIB_PATHS = -L$(CBT_LIB) 
LIBS = -lstdc++ -lm -lgzstream -lz 
endif 


OBJ_PATHS = -L$(CBT_OBJ) 





#=============================================================================
#	Compilation rules
#=============================================================================

#==============================================
#	CBT Libraries
#==============================================

# Make sure required library is up to date by running it's makefile. 
_cbtlib:
	cd $(CBT_ROOT); make 

# Make sure required library with debugging turned on
_cbtlibg:
	cd $(CBT_ROOT); make bug

# Make required library with profiling turned on. 
_cbtlibpg: 
	cd $(CBT_ROOT); make prof

#==============================================
#	Local Classes
#==============================================
# This is for code that is in a local class, that is a class which has not
# been put into the CBT++ or HGSC++ main source trees, or which is application specific
# Just list the classes you want to be compiled and linked in with your code
# on the libtargets line.   The assumption is that the .cc and .hh files for 
# this class are in the same directory as this Makefile. 
#
localobjs = $(libtargets:%=$(CBT_OBJ)/%.o)
glocalobjs = $(libtargets:%=$(CBT_OBJ)/%.g.o)
plocalobjs =$(libtargets:%=$(CBT_OBJ)/%.pg.o)

# I'm not quite sure why simply putting $(localobjs) in the dependency 
# list doesn't work.  
_localclasses: $(localobjs)
_glocalclasses: $(glocalobjs)
_plocalclasses: $(plocalobjs)

# Compile Local classes for debugging and profiling. 
$(CBT_OBJ)/%.pg.o: %.cc $(libdependencies)
	$(GCC) $(CCFLAGS) -p $(FASTFLAG) $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.pg.o $*.cc

# Compile Local classes for debugging
$(CBT_OBJ)/%.g.o: %.cc $(libdependencies)
	echo $%  $*
	$(GCC) $(CCFLAGS) -g $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.g.o $*.cc

# Compile Local classes optimized
$(CBT_OBJ)/%.o: %.cc $(libdependencies)
	$(GCC) $(CCFLAGS) $(FASTFLAG) $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.o $*.cc

#==============================================
#	Application compilation. 
#==============================================
# C++ programs are compiled then linked with libcbt (CBT++). 
# and any local object files.   Doing "make MyProgram"  will compile MyProgram.cc, 
# any local libraries, ensure that CBT++ libraries are up-to-date,
# and link everything together into an application called MyProgram.  "make MyProgram.g"  
# will do the same thing except it will make libraries with debugging turned on and the
# application created will be called MyProgram.g.   Similary, MyProgram.pg will be 
# an application with debugging and profiling (i.e. for gprof) turned on. 
#

#bob:
#	echo Bob

%: %.cc _cbtlib _localclasses
	$(GCC) $(CCFLAGS) $(FASTFLAG) $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.o $*.cc 
	$(GCC) $(LINKFLAGS) -o $(CBT_BIN)/$* $(LIB_PATHS) $(CBT_OBJ)/$*.o $(localobjs) $(CBT_LIB)/libcbt.a  $(applibs) $(LIBS)

%.g: %.cc _cbtlibg _glocalclasses
	$(GCC) $(CCFLAGS) -g $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.g.o $*.cc 
	$(GCC) -o $(CBT_BIN)/$*.g $(LIB_PATHS) $(CBT_OBJ)/$*.g.o $(glocalobjs) $(applibs) -lcbt.g -$(LIBS)

%.pg: %.cc _cbtlibpg _plocalclasses
	$(GCC) $(CCFLAGS) -O3 -p $(FASTFLAG) $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.pg.o $*.cc 
	$(GCC) -pg -o $(CBT_BIN)/$*.pg $(LIB_PATHS) $(CBT_OBJ)/$*.pg.o $(plocalobjs) $(applibs) $(CBT_LIB)/libcbt.pg.a  $(LIBS)


#==============================================
#	Application compilation dynamic link. 
#==============================================
# As much as we want static linking to be the default, 
# static linking in gcc does a quadradic time merge of symbols 
# across modules.   There is supposed to be a way to turn 
# this off, but it doesn't work without a patch.  See:
# 
# http://lists.boost.org/MailArchives/boost/msg53004.php
#
# In my case, the difference was 4 seconds vs 1 minute 30 seconds!!!
# 
# So, for normal compile/debug cycles, need to disable 
# static linking and rely on old dynamic link.  
# 
# Be careful!  Those shared libraries can bite you!! 
%.fastlink: %.cc _cbtlib _localclasses
	$(GCC) $(CCFLAGS) $(FASTFLAG) $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.o $*.cc 
	$(GCC) -o $(CBT_BIN)/$* $(LIB_PATHS) $(CBT_OBJ)/$*.o $(localobjs) $(CBT_LIB)/libcbt.a  -lstdc++ -lm -lgzstream -lz 

%.gfastlink: %.cc _cbtlibg _glocalclasses
	$(GCC) $(CCFLAGS) -g $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.g.o $*.cc 
	$(GCC) -o $(CBT_BIN)/$*.g $(LIB_PATHS) $(CBT_OBJ)/$*.g.o $(glocalobjs) -lcbt.g -lstdc++ -lm -lgzstream -lz 

%.pgfastlink: %.cc _cbtlibpg _plocalclasses
	$(GCC) $(CCFLAGS) -O3 -p $(FASTFLAG) $(INCLUDES_CC) -c -o $(CBT_OBJ)/$*.pg.o $*.cc 
	$(GCC) -pg -o $(CBT_BIN)/$*.pg $(LIB_PATHS) $(CBT_OBJ)/$*.pg.o $(plocalobjs) $(CBT_LIB)/libcbt.pg.a  -lstdc++ -lm -lgzstream -lz 







